﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing.Design;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.Design.WebControls;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;

namespace geoqualMenuBar
{
    [Serializable]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [ParseChildren(true)]
    public class MenuBarItem
    {
        private const string INVALIDCASTEXCEPTIONMESSAGE = "Not a MenuBar type";

        private string id = string.Empty;
        private string text = string.Empty;
        private string tooltip = string.Empty;
        private Unit width = Unit.Parse("0");
        private Image icon = null;
        private string iconurl = string.Empty;

        private MenuBars menuBarList;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        Editor(typeof(GQMenuBarCollectionEditor), typeof(UITypeEditor)),
        PersistenceMode(PersistenceMode.InnerProperty)]
        public MenuBars MenuBarItems
        {
            get
            {
                if (menuBarList == null) {
                    menuBarList = new MenuBars();
                }
                return menuBarList;
            }
        }

        [Category("Behavior"), DefaultValue(""),
        Description("Id of the menubaritem"), NotifyParentProperty(true)]
        public string ID
        {
            get { return id; }
            set { id = value; }
        }

        [Category("Behavior"), DefaultValue(""),
        Description("Text of the menubaritem"), NotifyParentProperty(true)]
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        [Category("Behavior"), DefaultValue(""),
        Description("Text of the menubaritem"), NotifyParentProperty(true)]
        public string Tooltip
        {
            get { return tooltip; }
            set { tooltip = value; }
        }

        [Category("Behavior"), DefaultValue(""),
        Description("Width of the menubaritem"), NotifyParentProperty(true)]
        public Unit Width {
            get { return width; }
            set { width = value; }
        }

        [Category("Appearance"), DefaultValue(""),
        Description("The icon url of the menubaritem"), NotifyParentProperty(true)]
        public string IconUrl {
            get { return iconurl; }
            set { iconurl = value; }
        }

        [Browsable(false)]
        public Image Icon {
            get { return icon; }
            set { icon = value; }
        }

        #region Constructors

        public MenuBarItem()
        {
            initialize(string.Empty, string.Empty, string.Empty, 0, null);
        }

        public MenuBarItem(string id, string text, int width = 0)
        {
            initialize(id, text, string.Empty, width, null);
        }

        public MenuBarItem(string id, string text, string tooltip, int width = 0)
        {
            initialize(id, text, tooltip, 0, null);
        }

        public MenuBarItem(string stitle, int width = 0)
        {
            initialize(stitle, stitle.Replace("_", " "), string.Empty, width, null);
        }

        public MenuBarItem(MenuBars stitles, int width = 0)
        {
            initialize(string.Empty, string.Empty, string.Empty, width, stitles);
        }

        public MenuBarItem(string id, string text, MenuBars stitles, int width = 0)
        {
            initialize(id, text, string.Empty, 0, stitles);
        }

        public MenuBarItem(string id, string text, string tooltip, MenuBars stitles, int width = 0)
        {
            initialize(id, text, tooltip, width, stitles);
        }

        public MenuBarItem(string stitle, MenuBars stitles, int width = 0)
        {
            initialize(stitle, stitle.Replace("_", " "), string.Empty, width, stitles);
        }

        #endregion

        private void initialize(string _id, string _text, string _tooltip, int w, MenuBars stitles)
        {
            id = _id;
            text = _text;
            tooltip = _tooltip;
            width = w;
            if (stitles != null)
                menuBarList = stitles;
        }
    }

    #region Collections

    [Serializable]
    public class MenuBars : Collection<MenuBarItem>
    {
        private Unit width = Unit.Parse("0");

        [Category("Behavior"), DefaultValue(""),
        Description("Width of the menubars"), NotifyParentProperty(true)]
        public Unit Width {
            get { return width; }
            set { width = value; }
        }

        public MenuBars() { }

        public MenuBars(MenuBars menubars)
        {
            foreach (MenuBarItem m in menubars) {
                this.Add(m);
            }
        }

        public MenuBars(MenuBarItem menubaritem)
        {
            this.Add(menubaritem);
        }

        public MenuBars RootMenu
        {
            get {
                if (this != null && this.Count == 1) {
                    return this[0].MenuBarItems;
                } else if (this != null) {
                    return this;
                }
                return null;
            }
        }
    }

    #endregion

}
